﻿namespace WMS6.Web
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Xml.Linq;
    using ExtAspNet;

	public class Default : BasePage
    {
        #region Controls
        /// <summary>
        /// mycss control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlLink mycss;

        /// <summary>
        /// form1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlForm form1;

        /// <summary>
        /// PageManager1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.PageManager PageManager1;

        /// <summary>
        /// RegionPanel1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.RegionPanel RegionPanel1;

        /// <summary>
        /// Region1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.Region Region1;

        /// <summary>
        /// Toolbar1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.Toolbar Toolbar1;

        /// <summary>
        /// ToolbarText1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.ToolbarText ToolbarText1;

        /// <summary>
        /// btnExpandAll control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.Button btnExpandAll;

        /// <summary>
        /// btnCollapseAll control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.Button btnCollapseAll;

        /// <summary>
        /// ToolbarTextWelcome control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.ToolbarText ToolbarTextWelcome;

        /// <summary>
        /// ToolbarFill1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.ToolbarFill ToolbarFill1;

        /// <summary>
        /// ToolbarTextLabel control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.ToolbarText ToolbarTextLabel;

        /// <summary>
        /// ddlSelectWh control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.DropDownList ddlSelectWh;

        /// <summary>
        /// btnExitSystem control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.Button btnExitSystem;

        /// <summary>
        /// Region2 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.Region Region2;

        /// <summary>
        /// treeMenu control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.Tree treeMenu;

        /// <summary>
        /// mainRegion control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.Region mainRegion;

        /// <summary>
        /// mainTabStrip control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::ExtAspNet.TabStrip mainTabStrip;

        /// <summary>
        /// XmlDataSource1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.XmlDataSource XmlDataSource1;

        /// <summary>
        /// hdnUserID control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlInputHidden hdnUserID;

        /// <summary>
        /// hdnCurrentWH control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlInputHidden hdnCurrentWH;

        /// <summary>
        /// hdnPerfLanguage control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlInputHidden hdnPerfLanguage;

        /// <summary>
        /// hdnFullLanguageOption control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlInputHidden hdnFullLanguageOption;

        /// <summary>
        /// henPerfStyle control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlInputHidden henPerfStyle;

        /// <summary>
        /// hdnLoginPageFrom control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlInputHidden hdnLoginPageFrom;

        #endregion

        private const string ChangeWhKey = "ChangeWh";
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["UserInfo"] == null)
            {
                base.Response.Redirect("Login.aspx");
            }
            else
            {
                if (!base.IsPostBack)
                {
                    this.LoadData();

                    //在取菜单的时候初始化session，写入当前仓库信息。
                    InitUserInfo();

                    XmlDataSource1.Data = GetMenuData();

                    treeMenu.Nodes.Clear();
                    // Bind xml to tree..
                    treeMenu.DataSource = XmlDataSource1;

                    treeMenu.DataBind();

                    BindWh();
                }
                if (Session[ChangeWhKey] != null)
                {
                    XmlDataSource1.Data = GetMenuData();
                    XmlDataSource1.DataFile = null;

                    this.treeMenu.Items.Clear();

                    // Bind xml to tree..
                    treeMenu.DataSource = XmlDataSource1;

                    treeMenu.DataBind();

                    Session[ChangeWhKey] = null;
                }
            }
        }

        private void BindWh()
        {
            DataSet dsWh = RoleFunctionServiceClientProxy.GetInstance().GetWHDataByUserID();
            DataTable dt = dsWh.Tables[0];
            DataColumn dataColumn = new DataColumn("WH_DB_ID_GLOBAL");
            dataColumn.DataType = typeof(string);
            dt.Columns.Add(dataColumn);
            foreach (DataRow row in dt.Rows)
            {
                row["WH_DB_ID_GLOBAL"] = row["WH_DB_ID"].ToString().Trim() + "+" + row["IS_GLOBAL"].ToString().Trim();
            }

            dt.AcceptChanges();

            this.ddlSelectWh.DataTextField = "WH_DB_DESC";
            this.ddlSelectWh.DataValueField = "WH_DB_ID_GLOBAL";

            this.ddlSelectWh.DataSource = dsWh.Tables[0];
            this.ddlSelectWh.DataBind();

            this.ddlSelectWh.SelectedValue = base.UserInfo.CurrentWH + "+" + base.UserInfo.IsGlobal;
        }

        protected void ddlSelectWh_SelectedChanged(object sender, EventArgs e)
        {
            InitUserInfo(this.ddlSelectWh.SelectedValue.Split('+')[0], this.ddlSelectWh.SelectedValue.Split('+')[1],
                         this.ddlSelectWh.SelectedText);

            Session[ChangeWhKey] = "ChangeWh";

            PageContext.Refresh();
        }

        private void LoadData()
        {
            this.henPerfStyle.Value = base.ChangeStylePattern(base.UserInfo.Preference.Themes);
            this.hdnPerfLanguage.Value = base.ChangeLangeugePattern(base.UserInfo.Preference.Lang);
            this.hdnFullLanguageOption.Value = base.UserInfo.Preference.Lang;
            if (base.UserInfo.Preference.Lang.ToUpper() == "ZH-CN")
            {
                this.ToolbarTextWelcome.Text = "欢迎 : " + base.UserInfo.UserName + "  &nbsp; &nbsp; &nbsp;当前登录仓库 : " + base.UserInfo.WhDbDesc;
            }
            else if (base.UserInfo.Preference.Lang.ToUpper() == "ZH-TW")
            {
                this.ToolbarTextWelcome.Text = "歡迎 : " + base.UserInfo.UserName + "  &nbsp; &nbsp; &nbsp;當前登錄倉庫 : " + base.UserInfo.WhDbDesc;
            }
            else
            {
                this.ToolbarTextWelcome.Text = "Welcome : " + base.UserInfo.UserName + "  &nbsp; &nbsp; &nbsp;Current Warehouse : " + base.UserInfo.WhDbDesc;
            }
     
        }

        private void InitUserInfo()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["WhName"]))
            {
                base.UserInfo.CurrentWH = base.Request.QueryString["WhName"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["isGlobal"]))
            {
                base.UserInfo.IsGlobal = base.Request.QueryString["isGlobal"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["WhDbDesc"]))
            {
                base.UserInfo.WhDbDesc = base.Request.QueryString["WhDbDesc"];
            }
        }

        private void InitUserInfo(string whName, string isGlobal, string whDbDesc)
        {
            if (!string.IsNullOrEmpty(whName))
            {
                base.UserInfo.CurrentWH = whName;
            }
            if (!string.IsNullOrEmpty(isGlobal))
            {
                base.UserInfo.IsGlobal = isGlobal;
            }
            if (!string.IsNullOrEmpty(whDbDesc))
            {
                base.UserInfo.WhDbDesc = whDbDesc;
            }
        }

        private string GetMenuData()
        {
            DataSet menuQueryEntity = RoleFunctionServiceClientProxy.GetInstance().GetRoleMenuData(base.UserInfo.UserID, base.UserInfo.IsGlobal, base.UserInfo.Preference.Lang);
            if ((menuQueryEntity != null) && (menuQueryEntity.Tables.Count > 0))
            {
                StringBuilder StrHtml = new StringBuilder();
                StrHtml.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                StrHtml.AppendLine("<Tree>");
                for (int i = 0; menuQueryEntity.Tables[0].Rows.Count > i; i++)
                {
                    if ((menuQueryEntity.Tables[0].Rows[i]["PARENT_GROUP_GID"].ToString() == "-1") && (menuQueryEntity.Tables[1].Rows.Count > 0))
                    {
                        int q = 0;
                        for (int m = 0; menuQueryEntity.Tables[1].Rows.Count > m; m++)
                        {
                            if (((menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[m]["A"].ToString()) && (!string.IsNullOrEmpty(menuQueryEntity.Tables[1].Rows[m]["C"].ToString()) && !string.IsNullOrEmpty(menuQueryEntity.Tables[1].Rows[m]["D"].ToString()))) && (q == 0))
                            {
                                StrHtml.AppendFormat("<TreeNode Text=\"{0}\" SingleClickExpand=\"true\">", menuQueryEntity.Tables[0].Rows[i]["TEXT"].ToString());

                                for (int j = 0; menuQueryEntity.Tables[0].Rows.Count > j; j++)
                                {
                                    if (menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString() == menuQueryEntity.Tables[0].Rows[j]["PARENT_GROUP_GID"].ToString())
                                    {
                                        int p = 0;
                                        for (int z = 0; menuQueryEntity.Tables[1].Rows.Count > z; z++)
                                        {
                                            if (((menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[z]["B"].ToString()) || (menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[z]["C"].ToString())) && (p == 0))
                                            {
                                                if (!string.IsNullOrEmpty(menuQueryEntity.Tables[0].Rows[j]["URL"].ToString()))
                                                {
                                                    StrHtml.AppendFormat("<TreeNode Text=\"{0}\" NavigateUrl=\"{1}\" Leaf=\"true\" IconUrl=\"{2}\" >",
                                                      menuQueryEntity.Tables[0].Rows[j]["TEXT"].ToString(),
                                                      menuQueryEntity.Tables[0].Rows[j]["URL"].ToString().Replace("~/", "") + "?FunctionId=" + menuQueryEntity.Tables[0].Rows[j]["FUNCTION_ID"].ToString(),
                                                      "");

                                                }
                                                else
                                                {
                                                    StrHtml.AppendFormat("<TreeNode Text=\"{0}\" SingleClickExpand=\"true\">", menuQueryEntity.Tables[0].Rows[j]["TEXT"].ToString());
                                                    StrHtml.AppendFormat("{0}", this.RecursiveMenu(menuQueryEntity.Tables[0].Rows[j]["GROUP_ID"].ToString(), menuQueryEntity));

                                                }
                                                StrHtml.AppendLine(" </TreeNode>");
                                                p++;
                                            }
                                        }
                                    }
                                }
                                StrHtml.AppendLine("</TreeNode>");
                                q++;
                            }
                        }
                    }
                }
                StrHtml.AppendLine("</Tree>");
                return StrHtml.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        private string RecursiveMenu(string GroupID, DataSet menuQueryEntity)
        {
            StringBuilder StrHtml = new StringBuilder();
            for (int i = 0; menuQueryEntity.Tables[0].Rows.Count > i; i++)
            {
                if (menuQueryEntity.Tables[0].Rows[i]["PARENT_GROUP_GID"].ToString() == GroupID)
                {
                    for (int j = 0; menuQueryEntity.Tables[1].Rows.Count > j; j++)
                    {
                        if (((GroupID == menuQueryEntity.Tables[1].Rows[j]["B"].ToString()) && (menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[j]["C"].ToString())) || ((GroupID == menuQueryEntity.Tables[1].Rows[j]["C"].ToString()) && (menuQueryEntity.Tables[0].Rows[i]["GROUP_ID"].ToString() == menuQueryEntity.Tables[1].Rows[j]["D"].ToString())))
                        {
                            StrHtml.AppendFormat("<TreeNode Text=\"{0}\" NavigateUrl=\"{1}\" Leaf=\"true\" IconUrl=\"{2}\" >",
                                                 menuQueryEntity.Tables[0].Rows[i]["TEXT"].ToString(),
                                                 menuQueryEntity.Tables[0].Rows[i]["URL"].ToString().Replace("~/", "") + "?FunctionId=" + menuQueryEntity.Tables[0].Rows[i]["FUNCTION_ID"].ToString(),
                                                 "");
                            StrHtml.AppendLine("</TreeNode>");

                            break;
                        }
                    }
                }
            }
            return StrHtml.ToString();
        }
	}
}
